/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package session;

import bean.Candidato;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import negocio.NegocioCandidato;

/**
 *
 * @author jcrbsa
 */
public class StartSessionCadastro extends HttpServlet {

  
        
   
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        
         HttpServletRequest req = (HttpServletRequest) request;
        NegocioCandidato fachada = NegocioCandidato.getInstance();
        HttpSession session = req.getSession(true);
        Candidato candidato = new Candidato();
        String cpf = req.getParameter("cpf");
        
        String nome = req.getParameter("nome");
        String email = req.getParameter("email");
        String senha = req.getParameter("senha");
        
       // candidato.setCpf(cpf);
        boolean valor =  fachada.procuraCPF(candidato);
        System.out.println(valor);
    
        if (cpf != null && cpf != "") {


            if ( valor  == true) {
                        
                    System.out.println("Já existe Candidato  - continua na mesma pagina");
                    req.setAttribute("existCPF", "CPF já cadastrado");
                    req.getRequestDispatcher("/index.jsp").forward(request, response);
                    
                }
            
            if (valor == false){
                  session.setAttribute("status", "logado ");
                  session.setAttribute("cpf", cpf.toString());
                  session.setAttribute("nome", nome.toString());
                  session.setAttribute("email", email.toString());
                  session.setAttribute("senha",senha.toString());
                  req.getRequestDispatcher("cadastroCandidato.jsp").forward(request, response);
                
            }
            
        }else{
           if ( valor  == true){
           req.setAttribute("existCPF", "CPF já cadastrado");
           }
           if(cpf == ""){
               req.setAttribute("existCPF", "*Campo Obrigatorio");
           }
            req.getRequestDispatcher("/index.jsp").forward(request, response);
        }

        }
    
   
    

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
